<?php
    function a1(){
        session_start();
        //假设用户登录成功获得了以下用户数据
        $userinfo = array(
            'uid'  => 10000,
            'name' => 'spark',
            'email' => 'spark@imooc.com',
            'sex'  => 'man',
            'age'  => '18'
        );
        header("content-type:text/html; charset=utf-8");

        /* 将用户信息保存到session中 */
        $_SESSION['uid'] = $userinfo['uid'];
        $_SESSION['name'] = $userinfo['name'];
        $_SESSION['userinfo'] = $userinfo;

        //* 将用户数据保存到cookie中的一个简单方法 */
        $secureKey = 'imooc'; //加密密钥
        $str = serialize($userinfo); //将用户信息序列化
        //用户信息加密前
        $str = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($secureKey), $str, MCRYPT_MODE_ECB));
        //用户信息加密后
        //将加密后的用户数据存储到cookie中
        setcookie('userinfo', $str);

        //当需要使用时进行解密
        $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($secureKey), base64_decode($str), MCRYPT_MODE_ECB);
        $uinfo = unserialize($str);
        echo "解密后的用户信息：<br>";
        print_r($uinfo);
    }

    a1();




//$sig = hash_hmac('sha256', $string, $secret)

$cars=array("Volvo","BMW","Toyota");
$userinfo = array(
    'uid'  => 10000,
    'name' => 'spark',
    'email' => 'spark@imooc.com',
    'sex'  => 'man',
    'age'  => '18'
);
$str = base64_encode(serialize($cars));
$ustr = base64_decode($str);

echo $str;
var_dump(unserialize($ustr));
?>